#include "StdAfx.h"
#include "GeomDocTree.h"
#include <memory>
#include "SketchPlane.h"
#include "GeomApp.h"
#include "MessageGeomUI.h"
#include "AnyDocument.h"
enum ICON_IDX
{
	ENTITY_ROOT_IDX = 0,
	SKETCH_PLANE_IDX,
	LAYER_IDX,
	PLANE_Z_IDX,
	PLANE_X_IDX,
	PLANE_Y_IDX

};
//////////////////////////////////////////////////////////////////////////
void TreeGroup::Restore(CGeomDocTree* pDocTree)
{
	ItemTraitList::iterator it = m_EntityData.begin();
	for(; it != m_EntityData.end(); ++it)
	{
		pDocTree->AddEntity(this, *it);
	}
}
//////////////////////////////////////////////////////////////////////////

CImageList CGeomDocTree::m_ImageList;
CGeomDocTree::IconIndexMap CGeomDocTree::m_mapIconIdx;
CGeomDocTree::CGeomDocTree(void)
:
//m_hEntityRoot(NULL),
//m_hCurLayer(NULL),
//m_hSketchPlane(NULL),
m_pCurADoc(NULL)
{
}

CGeomDocTree::~CGeomDocTree(void)
{
}
void CGeomDocTree::AddEntity(TreeGroup* pTreeGroup, const ItemTrait& itemTrait)
{
	TreeItemData* pData = new TreeItemData(TreeItemData::ENTITY);
	// add to tree
	HTREEITEM hSubItem = m_pTreeCtrl->InsertItem(itemTrait.szName, 
		m_mapIconIdx[itemTrait.nEditorId],
		m_mapIconIdx[itemTrait.nEditorId], 
		pTreeGroup->hCurLayer);
	m_pTreeCtrl->SetItemData(hSubItem, (DWORD_PTR)pData);
	m_pTreeCtrl->Expand(pTreeGroup->hCurLayer, TVE_EXPAND);
}
void CGeomDocTree::OnAddShape(CAnyDocument* pADoc, const ItemTrait& itemTrait)
{
	TreeGroup* pTreeGroup = m_mapDocTree[pADoc];
	pTreeGroup->AddItemData(itemTrait);

	AddEntity(pTreeGroup, itemTrait);
}
void CGeomDocTree::OnNewDocument(CAnyDocument* pADoc)
{
	// remove old from the tree
	//Warning! memory leak
	m_pTreeCtrl->DeleteAllItems();

	// create new item group
	TreeGroup* pTreeGroup = new TreeGroup;
	m_mapDocTree[pADoc] = pTreeGroup;
}
void CGeomDocTree::OnCloseDocument(CAnyDocument* pADoc)
{
	TreeGroup* pTreeGroup = m_mapDocTree[pADoc];
	m_pTreeCtrl->DeleteAllItems();
	m_pCurADoc = NULL;
}

void CGeomDocTree::OnActiveDocument(CAnyDocument* pADoc, bool bActive)
{
	if(bActive && m_pCurADoc!=pADoc)
	{
		m_pCurADoc = pADoc;
		m_pTreeCtrl->DeleteAllItems();
		TreeGroup* pTreeGroup = m_mapDocTree[pADoc];
		InitTreeItems(pTreeGroup);
		pTreeGroup->Restore(this);
	}
}

bool CGeomDocTree::InitTreeIcons(CImageList* pImageList)
{
	if(pImageList)
		return m_ImageList.Create(pImageList) == TRUE;
	
	return m_ImageList.Create(16,16,TRUE | ILC_COLOR8,10,5) == TRUE;
}
void CGeomDocTree::AddTreeItemIcon(UINT nID, HICON hIcon)
{
	m_mapIconIdx[nID]=m_ImageList.Add(hIcon);
}

namespace 
{
#define ADD_GENERAL_ITEM(hItem, hParent, szName, idxImage, nType)\
	{\
		hItem = m_pTreeCtrl->InsertItem(szName, idxImage, idxImage, hParent);\
		TreeItemData* pData = new TreeItemData(nType);\
		m_pTreeCtrl->SetItemData(hItem, (DWORD_PTR)pData);\
	}

#define ADD_NEW_PLANE(szName, idxImage, hParent, nData)\
	{\
		HTREEITEM hItem = m_pTreeCtrl->InsertItem(szName, idxImage, idxImage, hParent);\
		TreeItemData* pData = new TreeItemData(TreeItemData::PLANE);\
		pData->nID = nData;\
		m_pTreeCtrl->SetItemData(hItem, (DWORD_PTR)pData);\
	}

}
void CGeomDocTree::InitTreeItems(TreeGroup* pTreeGroup)
{
	// entity root
	ADD_GENERAL_ITEM(pTreeGroup->hEntityRoot, TVI_ROOT,_T("Entity"),
		ENTITY_ROOT_IDX, TreeItemData::ENTITY_ROOT);

	// layer
	ADD_GENERAL_ITEM(pTreeGroup->hCurLayer, 
		pTreeGroup->hEntityRoot,_T("Layer 0"),LAYER_IDX, TreeItemData::LAYER);
	m_pTreeCtrl->Expand(pTreeGroup->hEntityRoot, TVE_EXPAND);

	// plane root
	ADD_GENERAL_ITEM(pTreeGroup->hSketchPlane, TVI_ROOT,
		_T("Plane"),SKETCH_PLANE_IDX, TreeItemData::PLANE_ROOT);

	// xoy
	ADD_NEW_PLANE(_T("XOY"), PLANE_Z_IDX, pTreeGroup->hSketchPlane,CSketchPlane::STD_PLANE_XOY);

	// xoz
	ADD_NEW_PLANE(_T("XOZ"), PLANE_Y_IDX, pTreeGroup->hSketchPlane, CSketchPlane::STD_PLANE_XOZ);

	// yoz
	ADD_NEW_PLANE(_T("YOZ"), PLANE_X_IDX, pTreeGroup->hSketchPlane, CSketchPlane::STD_PLANE_YOZ);
	m_pTreeCtrl->Expand(pTreeGroup->hSketchPlane, TVE_EXPAND);

}
bool CGeomDocTree::ApplyIconsToTree()
{
	ASSERT(m_pTreeCtrl);
	m_pTreeCtrl->SetImageList(&m_ImageList, TVSIL_NORMAL);
	return true;
}

void CGeomDocTree::SetTreeCtrl(CTreeCtrl* pTreeCtrl)
{
	m_pTreeCtrl = pTreeCtrl;

}

void CGeomDocTree::OnSelectChanged()
{
	HTREEITEM hItem = m_pTreeCtrl->GetSelectedItem();
	if(hItem == NULL)
		return;

	DWORD_PTR ptrData = m_pTreeCtrl->GetItemData(hItem);
	TreeItemData* pItemData = (TreeItemData*)ptrData;
	if(pItemData == NULL)
		return;

	switch(pItemData->nItemType)
	{
	case TreeItemData::PLANE:
		ActivePlane(pItemData->nID);
		break;
	case TreeItemData::ENTITY:
		ActiveEntity(pItemData->nID);
		break;
	default:
		break;
	}
}

void CGeomDocTree::ActivePlane(UINT nID)
{
	CView* pView = GetActiveView();
	if(pView == NULL)
		return;

	::SendMessage(pView->GetSafeHwnd(), WM_GEOM_PLANE, GPM_NEW_PLANE, nID);
}

void CGeomDocTree::ActiveEntity(UINT nID)
{

}